#include "Subject.h"
#include <QMetaObject>

#include ".\UI\ModRun\Output\FactoryOutput.h"

Subject::Subject()
{
    m_listObs.clear();
    m_strName = "";
}

Subject::~Subject()
{

}

//添加观察者;
void Subject::RegiserObserver(std::shared_ptr<IObserver> pOb)
{
    //unique_ptr时 不Move会报C2280错误;
    //m_listObs.push_back(std::move(pOb));
    QString strOutput = m_strName + "->添加观察者:" + pOb->GetName();
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    m_listObs.push_back(pOb);
}

//移除观察者;
void Subject::RemoveObserver(std::shared_ptr<IObserver> pOb)
{
    //unique_ptr时 不Move会报C2280错误;
    //m_listObs.remove(std::move(pOb));
    QString strOutput = m_strName + "->移除观察者:" + pOb->GetName();
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    m_listObs.remove(pOb);
}

//发送通知;
void Subject::Notify(QString strStatus)
{
    QString strOutput = m_strName + "->广播状态:" + strStatus;
    qDebug() << strOutput;
    std::shared_ptr<IOutput> pOutPut = FactoryOutput::GetInstance()->CreateRunOutput();
    if (pOutPut)
    {
        std::initializer_list<QString> outList{ strOutput };
        pOutPut->OutPut(outList);
    }

    for (const std::shared_ptr<IObserver> &obj : m_listObs)
    {
        if (obj)
        {
            (obj)->Update(strStatus);
        }
    }
}
